#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from xpy3lib.XRetryableQuery import XRetryableQuery
from xpy3lib.LoggableObject import LoggableObject
from xpy3lib.XLogger import XLogger

class SO4C2(LoggableObject):
    """

    """
    db_conn = None
    spec_bz = None
    m_cent = None
    qs_code = None

    success = False
    df_range_4_all = None

    # TIMES = 0

    def __init__(self, p_db_conn=None, p_spec_bz=None, p_m_cent=None, p_qs_code=None):
        """
        S04Rnge那里，SQL，MIDF00，S是要传进去的参数
        p_m_cent
        QS_CODE
        """
        self.dic_range_4_all = dict()

        self.db_conn = p_db_conn
        self.spec_bz = p_spec_bz
        self.m_cent = p_m_cent
        if p_qs_code is None or p_qs_code == '' or p_qs_code.startswith("foo_"):
            p_qs_code = None
        self.qs_code = p_qs_code
        XLogger.getInstance().logger.info(self.qs_code)

        # SO4C2.TIMES += 1

        sql = " SELECT " \
              " FIELD_LG," \
              " SPEC_BZ," \
              " RANGE_N," \
              " RANGE_COD," \
              " PROD1," \
              " LOWER_V," \
              " SUPER_V " \
              " FROM " \
              " BGRAGGCB.WH_JH00_ZSPEC2" \
              " WHERE 1=1 " \
              " AND SPEC_BZ='%s'" \
              " AND m_cent='%s'" % (p_spec_bz, p_m_cent)
        # NOTE 动态sql语句
        if p_qs_code is not None:
            sql = "%s AND PROD1='%s' " % (sql, self.qs_code)
        else:
            sql = "%s AND PROD1 is null" % (sql)
        XLogger.getInstance().logger.info(sql)
        df = XRetryableQuery(p_db_conn=p_db_conn, p_sql=sql, p_max_times=5).redo()
        df.columns = df.columns.str.upper()
        success = df.empty is False

        self.df_range_4_all = df
        self.success = success

        # if self.success is False and SO4C2.TIMES <= 2:
        #     so = SO4C2(p_db_conn=self.conn_dbprod7, p_spec_bz=self.spec_bz, p_m_cent=self.m_cent, p_qs_code=None)
        #     self.success = so.success
        #     self.df_range_4_all = so.df_range_4_all
        #     SO4C2.TIMES = 0

    def get_all_c2_by(self):
        """
        :return:
        """
        field_lg_property = self.df_range_4_all['FIELD_LG'][0] if self.success else None
        return self.success, field_lg_property

    def get_matched_c2_by(self, p_field_lg_value=None):
        """
        
        :param p_field_lg_value:
        :return: success, row, field_lg_property(name, not value), range_cod, range_n
        """
        if self.success:
            field_lg_property = self.df_range_4_all['FIELD_LG'][0]
        else:
            field_lg_property = None

        t = (False, None, field_lg_property, '', '')
        if field_lg_property is not None:
            for index, row in self.df_range_4_all.iterrows():
                #XLogger.getInstance().logger.info(float(row['LOWER_V']))
                #XLogger.getInstance().logger.info(float(row['SUPER_V']))
                if p_field_lg_value >= float(row['LOWER_V']) and p_field_lg_value < float(row['SUPER_V']):
                    t = (True, row, field_lg_property, row['RANGE_COD'], row['RANGE_N'])
                    break
        return t
